//
//  HEMainViewController.m
//  huien
//
//  Created by yiban on 2017/12/8.
//  Copyright © 2017年 lcb. All rights reserved.
//

#import "HEMainViewController.h"
#import "RKTabView.h"
#import "HEIndexViewController.h"
#import "HEInvestViewController.h"
#import "HEMeViewController.h"

@interface HEMainViewController ()<RKTabViewDelegate>
{
    
}
@property (nonatomic,strong)RKTabView *tabView;
@property (nonatomic,strong)UIView *showArea;
@property(nonatomic,strong)HEIndexViewController *indexVC;
@property(nonatomic,strong)HEInvestViewController *investVC;
@property(nonatomic,strong)HEMeViewController *meVC;
@property(nonatomic,strong)UIViewController *currentVC;
@property(nonatomic,assign)NSInteger currentIndex;
@end

@implementation HEMainViewController
@synthesize tabView=_tabView;
@synthesize currentVC=_currentVC;
@synthesize currentIndex=_currentIndex;
- (instancetype)init
{
    self = [super init];
    if (self) {
       
        
    }
    return self;
}
- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.headerView.hidden=YES;
    self.currentIndex=0;
    // 初始化控制器
    for (UIViewController *vc in self.childViewControllers) {
        [vc willMoveToParentViewController:nil];
        [vc.view removeFromSuperview];
        [vc removeFromParentViewController];
    }
    for (UIView *view in self.view.subviews) {
        [view removeFromSuperview];
    }
    [self controllersInit];
    [self initUI];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}
-(void)controllersInit
{
    // 初始化两个控制器并作为root控制器的subController
    _indexVC =[[HEIndexViewController alloc] init];
    [self addChildViewController:self.indexVC];
    [self.indexVC didMoveToParentViewController:self];
    //
    _investVC =[[HEInvestViewController alloc] init];
    [self addChildViewController:self.investVC];
    [self.investVC didMoveToParentViewController:self];
    //
    _meVC =[[HEMeViewController alloc] init];
    [self addChildViewController:self.meVC];
    [self.meVC didMoveToParentViewController:self];
    
}

-(void)initUI
{
    _showArea = [[UIView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT-YB_TabbarHeight)];
    _showArea.layer.masksToBounds = YES;
    [self.view addSubview:_showArea];
    //
    RKTabItem *tabItem1 = [RKTabItem createUsualItemWithImageEnabled:[UIImage imageNamed:@"tab_home_selected"] imageDisabled:[UIImage imageNamed:@"tab_home_normal"]];
    tabItem1.titleString=@"首页";
    tabItem1.titleFontColor=UIColorFromRGB(0xcdcccd);
    tabItem1.titleFontColorEnabled =UIColorFromRGB(0x00a5ed);
    tabItem1.titleFont=[UIFont boldSystemFontOfSize:13.0f];
    tabItem1.tabState = TabStateEnabled;
    RKTabItem *tabItem2 = [RKTabItem createUsualItemWithImageEnabled:[UIImage imageNamed:@"tab_graph_selected"] imageDisabled:[UIImage imageNamed:@"tab_graph_normal"]];
    tabItem2.titleString=@"投资";
    tabItem2.titleFontColor=UIColorFromRGB(0xcdcccd);
    tabItem2.titleFontColorEnabled =UIColorFromRGB(0x00a5ed);
    tabItem2.titleFont=[UIFont boldSystemFontOfSize:13.0f];
    RKTabItem *tabItem3 = [RKTabItem createUsualItemWithImageEnabled:[UIImage imageNamed:@"tab_user_selected"] imageDisabled:[UIImage imageNamed:@"tab_user_normal"]];
    tabItem3.titleString=@"我的";
    tabItem3.titleFontColor=UIColorFromRGB(0xcdcccd);
    tabItem3.titleFontColorEnabled =UIColorFromRGB(0x00a5ed);
    tabItem3.titleFont=[UIFont boldSystemFontOfSize:13.0f];
    _tabView = [[RKTabView alloc] initWithFrame:CGRectMake(0, SCREEN_HEIGHT-YB_TabbarHeight, SCREEN_WIDTH, YB_TabbarHeight) andTabItems:@[tabItem1, tabItem2, tabItem3]];
    _tabView.backgroundColor=UIColorFromRGB(0xffffff);
    _tabView.drawSeparators=YES;
    _tabView.lowerSeparatorLineColor=UIColorFromRGB(0xffffff);
    _tabView.upperSeparatorLineColor=UIColorFromRGB(0xcdcccd);
    _tabView.delegate=self;
    [self.view addSubview:_tabView];
    [self.view bringSubviewToFront:_tabView];
    //初始化首页
    // 将第一个控制器的view添加进来展示
    [_showArea addSubview:self.indexVC.view];
    _currentVC = self.indexVC;
}
//Called Only for unexcludable items. (TabTypeUnexcludable)
- (void)tabView:(RKTabView *)tabView tabBecameDisabledAtIndex:(NSUInteger)index tab:(RKTabItem *)tabItem{}
//Called for all types except TabTypeButton
- (void)tabView:(RKTabView *)tabView tabBecameEnabledAtIndex:(NSUInteger)index tab:(RKTabItem *)tabItem
{
    self.currentIndex =index;
    switch (index) {
        case 0:{
            
            [self transitionFromViewController:_currentVC
                              toViewController:self.indexVC
                                      duration:0
                                       options:UIViewAnimationOptionTransitionNone
                                    animations:^{
                                        
                                    }
                                    completion:^(BOOL finished) {
                                        _currentVC = self.indexVC;
                                    }];
         
        }
            break;
        case 1:{
             [[NSNotificationCenter defaultCenter] postNotificationName:@"tabChanage111" object:@"1"];
            [self transitionFromViewController:_currentVC
                              toViewController:self.investVC
                                      duration:0
                                       options:UIViewAnimationOptionTransitionNone
                                    animations:^{
                                        
                                    }
                                    completion:^(BOOL finished) {
                                        _currentVC = self.investVC;
                                    }];
        }
            break;
        case 2:
        {
             [[NSNotificationCenter defaultCenter] postNotificationName:@"tabChanage222" object:@"1"];
            [self transitionFromViewController:_currentVC
                              toViewController:self.meVC
                                      duration:0
                                       options:UIViewAnimationOptionTransitionNone
                                    animations:^{
                                        
                                    }
                                    completion:^(BOOL finished) {
                                        _currentVC = self.meVC;
                                    }];
        }
            break;
        default:
            break;
    }
}
//进入主页
-(void)setSelectTable:(NSInteger)index
{
    if (self.tabView) {
        [self.tabView switchTabIndex:index];
    }
}
@end
